﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebCommit.Common.Entitys;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using WebCommit.Common.Helpers;
using WebCommit.Common.Enums;
using System.Threading;

namespace WebCommit.Crawler
{
	public class WebCrawlWorker:Worker
	{
		public WebCrawlWorker(List<WebResourceEntity> webList,object lockObj)
			: base(webList, lockObj)
		{ }

		private object lockObj = new object();
		public override List<WebResourceEntity> PreWork(List<WebResourceEntity> webList)
		{
			List<WebResourceEntity> list = new List<WebResourceEntity>();
			lock (WebListLockObj)
			{
				WebResourceEntity web = webList.FirstOrDefault(w => (w.WebCrawlStatus == CrawlStatus.Pending && w.UrlCrawlStatus != CrawlStatus.Crawling));
				if (web != null)
				{
					web.WebCrawlStatus = CrawlStatus.Crawling;
					list.Add(web);
				}
			}
			if (list.Count == 0)
				Thread.Sleep(10);

			return list;
		}

		public override List<WebResourceEntity> DoWork(List<WebResourceEntity> webList)
		{
			if (webList.Count < 1)
				return null;
			WebResourceEntity web = webList[0];
			HttpWebResponse resp = null;
			try
			{
				HttpWebRequest req = Utility.CreateWebRequest(web.Url);
				resp = (HttpWebResponse)req.GetResponse();
				if (resp.ContentType.ToLower().IndexOf("text/html") < 0)
				{
					lock (WebListLockObj)
					{
						web.WebCrawlStatus = CrawlStatus.Cancel;
					}
					Utility.Log("Cancel: ContentType= " + resp.ContentType, null);
					return webList;
				}
				string charSet = "";
				string title = Parser.ExtractTitle(resp, ref charSet);
				lock (WebListLockObj)
				{
					web.Title = title;
					web.CharacterSet = charSet;
					web.WebCrawlStatus = CrawlStatus.OK;
				}
				return webList;
			}
			catch (Exception ex)
			{
				lock (WebListLockObj)
				{
					web.WebCrawlStatus = CrawlStatus.Error;
					web.ErrorMsg = ex.Message;
				}
				Utility.Log("Error:" + web.Url, ex);
				return webList;
			}
			finally
			{
				if (resp != null)
					resp.Close();
			}

		}

	}
}
